home *** CD-ROM | disk | FTP | other *** search
/ T&A 2 the Maxx 3 / T and A 2 The Maxx Number 3.iso / viewers / unixview / xgiftar.z / xgiftar / new.c < prev    next >
C/C++ Source or Header  |  1991-05-20  |  4KB  |  210 lines

  1. /* new.c:
  2.  *
  3.  * functions to allocate and deallocate structures and structure data
  4.  *
  5.  * jim frost 09.29.89
  6.  *
  7.  * Copyright 1989, 1991 Jim Frost.
  8.  * See included file "copyright.h" for complete copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "image.h"
  13.  
  14. extern int _Xdebug;
  15.  
  16. /* this table is useful for quick conversions between depth and ncolors
  17.  */
  18.  
  19. unsigned long DepthToColorsTable[] = {
  20.   /*  0 */ 1,
  21.   /*  1 */ 2,
  22.   /*  2 */ 4,
  23.   /*  3 */ 8,
  24.   /*  4 */ 16,
  25.   /*  5 */ 32,
  26.   /*  6 */ 64,
  27.   /*  7 */ 128,
  28.   /*  8 */ 256,
  29.   /*  9 */ 512,
  30.   /* 10 */ 1024,
  31.   /* 11 */ 2048,
  32.   /* 12 */ 4096,
  33.   /* 13 */ 8192,
  34.   /* 14 */ 16384,
  35.   /* 15 */ 32768,
  36.   /* 16 */ 65536,
  37.   /* 17 */ 131072,
  38.   /* 18 */ 262144,
  39.   /* 19 */ 524288,
  40.   /* 20 */ 1048576,
  41.   /* 21 */ 2097152,
  42.   /* 22 */ 4194304,
  43.   /* 23 */ 8388608,
  44.   /* 24 */ 16777216,
  45.   /* 25 */ 33554432,
  46.   /* 26 */ 67108864,
  47.   /* 27 */ 134217728,
  48.   /* 28 */ 268435456,
  49.   /* 29 */ 536870912,
  50.   /* 30 */ 1073741824,
  51.   /* 31 */ 2147483648,
  52.   /* 32 */ 2147483648 /* bigger than unsigned int; this is good enough */
  53. };
  54.  
  55. unsigned long colorsToDepth(ncolors)
  56.      unsigned long ncolors;
  57. { unsigned long a;
  58.  
  59.   for (a= 0; (a < 32) && (DepthToColorsTable[a] < ncolors); a++)
  60.     /* EMPTY */
  61.     ;
  62.   return(a);
  63. }
  64.  
  65. char *dupString(s)
  66.      char *s;
  67. { char *d;
  68.  
  69.   if (!s)
  70.     return(NULL);
  71.   d= (char *)lmalloc(strlen(s) + 1);
  72.   strcpy(d, s);
  73.   return(d);
  74. }
  75.  
  76. void newRGBMapData(rgb, size)
  77.      RGBMap       *rgb;
  78.      unsigned int  size;
  79. {
  80.   rgb->used= 0;
  81.   rgb->size= size;
  82.   rgb->compressed= 0;
  83.   rgb->red= (Intensity *)lmalloc(sizeof(Intensity) * size);
  84.   rgb->green= (Intensity *)lmalloc(sizeof(Intensity) * size);
  85.   rgb->blue= (Intensity *)lmalloc(sizeof(Intensity) * size);
  86. }
  87.  
  88. void freeRGBMapData(rgb)
  89.      RGBMap *rgb;
  90. {
  91.   lfree((byte *)rgb->red);
  92.   lfree((byte *)rgb->green);
  93.   lfree((byte *)rgb->blue);
  94. }
  95.  
  96. Image *newBitImage(width, height)
  97.      unsigned int width, height;
  98. { Image        *image;
  99.   unsigned int  linelen;
  100.  
  101.   image= (Image *)lmalloc(sizeof(Image));
  102.   image->type= IBITMAP;
  103.   image->title= NULL;
  104.   newRGBMapData(&(image->rgb), (unsigned int)2);
  105.   *(image->rgb.red)= *(image->rgb.green)= *(image->rgb.blue)= 65535;
  106.   *(image->rgb.red + 1)= *(image->rgb.green + 1)= *(image->rgb.blue + 1)= 0;
  107.   image->rgb.used= 2;
  108.   image->width= width;
  109.   image->height= height;
  110.   image->depth= 1;
  111.   linelen= (width / 8) + (width % 8 ? 1 : 0); /* thanx johnh@amcc.com */
  112.   image->data= (unsigned char *)lcalloc(linelen * height);
  113.   return(image);
  114. }
  115.  
  116. Image *newRGBImage(width, height, depth)
  117.      unsigned int width, height, depth;
  118. { Image        *image;
  119.   unsigned int  pixlen, numcolors, a;
  120.  
  121.   pixlen= (depth / 8) + (depth % 8 ? 1 : 0);
  122.   if (pixlen == 0) /* special case for `zero' depth image, which is */
  123.     pixlen= 1;     /* sometimes interpreted as `one color' */
  124.   numcolors = depthToColors(depth);
  125.   image= (Image *)lmalloc(sizeof(Image));
  126.   image->type= IRGB;
  127.   image->title= NULL;
  128.   newRGBMapData(&(image->rgb), numcolors);
  129.   image->width= width;
  130.   image->height= height;
  131.   image->depth= depth;
  132.   image->pixlen= pixlen;
  133.   image->data= (unsigned char *)lmalloc(width * height * pixlen);
  134.   return(image);
  135. }
  136.  
  137. Image *newTrueImage(width, height)
  138.      unsigned int width, height;
  139. { Image        *image;
  140.   unsigned int  pixlen, numcolors, a;
  141.  
  142.   image= (Image *)lmalloc(sizeof(Image));
  143.   image->type= ITRUE;
  144.   image->title= NULL;
  145.   image->rgb.used= image->rgb.size= 0;
  146.   image->width= width;
  147.   image->height= height;
  148.   image->depth= 24;
  149.   image->pixlen= 3;
  150.   image->data= (unsigned char *)lmalloc(width * height * 3);
  151.   return(image);
  152. }
  153.  
  154. void freeImageData(image)
  155.      Image *image;
  156. {
  157.   if (image->title) {
  158.     lfree((byte *)image->title);
  159.     image->title= NULL;
  160.   }
  161.   if (!TRUEP(image))
  162.     freeRGBMapData(&(image->rgb));
  163.   lfree(image->data);
  164. }
  165.  
  166. void freeImage(image)
  167.      Image *image;
  168. {
  169.   freeImageData(image);
  170.   lfree((byte *)image);
  171. }
  172.  
  173. byte *lmalloc(size)
  174.      unsigned int size;
  175. { byte *area;
  176.  
  177.   if (size == 0) {
  178.     size= 1;
  179.     if (_Xdebug)
  180.       fprintf(stderr, "lcalloc given zero size!\n");
  181.   }
  182.   if (!(area= (byte *)malloc(size))) {
  183.     memoryExhausted();
  184.     /* NOTREACHED */
  185.   }
  186.   return(area);
  187. }
  188.  
  189. byte *lcalloc(size)
  190.      unsigned int size;
  191. { byte *area;
  192.  
  193.   if (size == 0) {
  194.     size= 1;
  195.     if (_Xdebug)
  196.       fprintf(stderr, "lcalloc given zero size!\n");
  197.   }
  198.   if (!(area= (byte *)calloc(1, size))) {
  199.     memoryExhausted();
  200.     /* NOTREACHED */
  201.   }
  202.   return(area);
  203. }
  204.  
  205. void lfree(area)
  206.      byte *area;
  207. {
  208.   free(area);
  209. }
  210.